package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tbit.uqbike.constant.GatewayProtocol;
import com.tbit.uqbike.object.business.TerControlOrderDownRsp;
import com.tbit.uqbike.object.business.TerControlOrderTerRsp;
import com.tbit.uqbike.object.pojo.TerAlarm;
import com.tbit.uqbike.object.pojo.TerControlMsg;
import com.tbit.uqbike.service.business.TerControlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class QueueListenter implements MessageListener {
    @Autowired
    private TerControlService terControlService;

    @Override
    @RabbitListener(queues = {"${mq.queue.tbitUqbikeWebManager}"}, concurrency = "5")
    public void onMessage(Message msg) {
        try {
            String respMsg = new String(msg.getBody(), "UTF-8");
            log.info(String.format("接收到mq消息:%s", respMsg));
            /**判断消息类型*/
            JSONObject object = JSON.parseObject(respMsg);
            int msgId = object.getIntValue(TerControlMsg.FEILD_MSGID);
            String dataStr = object.getString(TerControlMsg.FEILD_DATA);

            switch (msgId) {
                case GatewayProtocol.MQ_MSG_ID_ALARM_PUSH:
                    if (StrUtil.isNotBlank(dataStr)) {
                        // 告警推送
                        TerAlarm terAlarm = JSONObject.parseObject(dataStr, TerAlarm.class);
                        if (terAlarm != null) {
                            // terAlarmService.recAlarm(terAlarm);
                        }
                    }
                    break;
                case GatewayProtocol.MQ_MSG_ID_CONTROL_DOWN_RSP:
                    // 发送结果回复
                    if (StrUtil.isNotBlank(dataStr)) {
                        //
                        TerControlOrderDownRsp rsp = JSONObject.parseObject(dataStr, TerControlOrderDownRsp.class);
                        log.info("rsp============={}", JSONObject.toJSONString(rsp));

                        if (rsp != null) {
                            terControlService.sendRspHandle(rsp.serNO, rsp.controlRet);
                        }
                    }
                    break;
                case GatewayProtocol.MQ_MSG_ID_CONTROL_TER_RSP:
                    // 终端回复
                    if (StrUtil.isNotBlank(dataStr)) {
                        TerControlOrderTerRsp rsp = JSONObject.parseObject(dataStr, TerControlOrderTerRsp.class);
                        log.info("ter============={}", JSONObject.toJSONString(rsp));
                        if (rsp != null && StrUtil.isNotBlank(rsp.serNO)) {
                            terControlService.respHandle(rsp.serNO, rsp.controlRet, rsp.paramRet, rsp.picList);
                        }
                    }
                    break;
                default:
                    log.warn("未知消息类型={}", msgId);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

    }

}
